//模块配置信息
require.config({
	//baseUrl: "./",
	paths: {
		//工具模块
		'less': 'tools/less.min',
		'jquery': 'tools/jquery.min',
		'bootstrap': 'tools/bootstrap.min',
		'handlebars': 'tools/handlebars',

		//配置模块
		'config': './config',
		"appConfig":"./appConfig",

		'top_bar': 'modules/top_bar',
		'problem': 'modules/problem',
		'showBigImg': 'modules/showBigImg',
		'userInfo': 'modules/userInfo',
		'ask': 'modules/ask',
		'modal_template': 'modules/modal_template',
		'cookie': 'modules/cookie',
		'check_log': 'modules/check_log',
		'login': 'modules/login',
		'other_log': 'modules/login',
		'reg': 'modules/reg',
		'user': 'modules/user',
	},
	skim: {  //配置依赖关系与接口
		"bootstrap": {	// bootstrap依赖jquery库
			deps: ['jquery'],
			exports: 'bootstrap'
		},
		"handlebars": {
			exports: 'Handlebars'
		},
		"user": {
			deps: ['modal_template']
		}
	}
});


//加载LESS, jquery, bootstrap
require(['less', 'jquery'], function(){
	require(['bootstrap']);	
});

//加载顶栏top_bar模块
require(['top_bar'], function(top_bar){
	top_bar();
});

//加载中心问题模块，并用Hanlebars生成
require(['problem', 'cookie'], function(problem, cookie){
	// var date = new Date();
	// var timestamp = date.getTime();
	var userId = cookie.getCookie('userId');
	
	//首次加载(skip, total, userId, callback)------没有使用时间戳
	problem.getProblemList(0, 10, userId);
	problem.clickZan();

	//跳过首次加载的10
	var skip = 10,
		theLast = false;

	//滚动到底部，加载更多
	$(document).ready(function(){
		var loading = false;
		$(window).scroll(function(){
			var document_height = $(document).height();
			var window_height = $(window).height();
			var scroll_top = $(document).scrollTop();

			if (scroll_top >= (document_height - window_height -50) && !loading && !theLast){
				loading = true;
				console.log('loading!' + skip)
				//根据全局sorting与skip，判断加载方式
				problem.getProblemList(skip, 10, userId, function(nums){
					//回调中修改参数
					skip += 10;
					loading = false;
					if (nums == 0){
						theLast = true;
					}
				});
			}
		});

	});

});

//右方个人信息模块
require(['userInfo'], function(userInfo){
	userInfo.hasAvatar();
});

//加载提问模块
require(['jquery'], function($){
	var load_times = 0;
	$(document).on('click', '.ask-button', function(){
		if (load_times == 0){
			load_times ++;
			//加载提问模块
			require(['ask'], function(ask){
				ask();
			});
		}
	});
});